package com.kaisa.activity.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.kaisa.activity.R;
import com.kaisa.activity.adapter.ShiListAdapter;
import com.kaisa.activity.bean.ShipListBean;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class ShipListActivity extends AppCompatActivity {
    public static final String PATH_SHIPLIST = "http://s.caissa.com.cn/WS/appsearch.ashx?action=QueryFor3g&param=%3CQuery%3E%3Ckeyword%3E%E9%82%AE%E8%BD%AE%3C/keyword%3E%3CPagesize%3E10%3C/Pagesize%3E%3Cpageindex%3E1%3C/pageindex%3E%3Cformat%3Ejson%3C/format%3E%3Ccompany%3E%E5%8C%97%E4%BA%AC%E7%AB%99%3C/company%3E%3C/Query%3E";

    @ViewInject(R.id.iv_shiplist_back)
    private ImageView back;
    @ViewInject(R.id.lv_shiplist)
    private PullToRefreshListView shipListView;
    private List<ShipListBean.EntitysEntity> list = new ArrayList<ShipListBean.EntitysEntity>();
    private ShiListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shiplist);
        x.view().inject(this);

        initView();
        initData();
        addListener();
    }

    private void initView() {
        adapter = new ShiListAdapter(list,this);
        shipListView.setAdapter(adapter);
    }

    private void initData() {
        x.http().get(getParams(), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                ShipListBean bean = JSONObject.parseObject(s,ShipListBean.class);
                list.addAll(bean.getEntitys());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    private void addListener() {
        //滑到低端的监听
        shipListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
                initData();
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        shipListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                /*Intent intent = new Intent(AllGoActivity.this,AllGoDetailsActivity.class);
                intent.putExtra("productId",list.get(i).getProduct_id());
                startActivity(intent);*/
            }
        });

    }

    public RequestParams getParams() {

        RequestParams params = new RequestParams(PATH_SHIPLIST);

        return params;
    }

}
